package com.jnu.student.data;
import android.content.Context;
import android.util.Log;
import java.io.*;
import java.util.ArrayList;

public class BillDataBank {
    private static final String DATA_FILENAME = "billData.data";

    public ArrayList<Bill> loadBills(Context applicationContext) {
        ArrayList<Bill> data = new ArrayList<>();
        try {
            FileInputStream fis = applicationContext.openFileInput(DATA_FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            data = (ArrayList<Bill>) ois.readObject();
            ois.close();
            fis.close();
            Log.d("Serialization", "Data loaded successfully. Item count: " + data.size());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return data;
    }

    public void saveBills(Context context, ArrayList<Bill> bills) {
        try {
            FileOutputStream fos = context.openFileOutput(DATA_FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(bills);
            oos.close();
            fos.close();
            Log.d("Serialization", "Data serialized and saved to file.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}